package com.jinke.uang.ui.activity.mine;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jinke.uang.R;
import com.jinke.uang.base.BaseNormalActivity;
import com.jinke.uang.base.BaseResponse;
import com.jinke.uang.common.RouterMap;
import com.jinke.uang.common.NoDoubleClickListener;
import com.jinke.uang.bean.BindedCardData;
import com.jinke.uang.constant.HttpConstant;
import com.jinke.uang.model.MineModule;
import com.jinke.uang.presenter.CommonPresenter;
import com.jinke.uang.utils.MaskUtils;
import com.jinke.uang.utils.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 我的银行卡
 */
@Route(name = "个人信息", path = RouterMap.MINE_BANK_CARD)
public class BankCardActivity extends BaseNormalActivity <CommonPresenter,MineModule> {

    @BindView(R.id.tv_bank_card_name)
    TextView tv_bank_card_name;
    @BindView(R.id.tv_bank_card_num)
    TextView tv_bank_card_num;
    @BindView(R.id.tv_bank_card_user_name)
    TextView tv_bank_card_user_name;
    @BindView(R.id.tvChange)
    TextView tvChange;
    @BindView(R.id.linYesData)
    LinearLayout linYesData;
    @BindView(R.id.linNoData)
    LinearLayout linNoData;

    private BindedCardData bindedCardData;

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void onUIReady() {
        tvChange.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                super.onNoDoubleClick(v);
                ARouter.getInstance().build(RouterMap.BANK_AUTH_PAGE).withBoolean("is_change",
                        true).navigation();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        showLoadingView();
        mPresenter.getData(HttpConstant.BINDED_CARD_DATA,null);
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_bank_card;
    }

    @Override
    public String getPageTitle() {
        return getStringResource(R.string.personal_mine_card);
    }

    @Override
    protected CommonPresenter getPresenter() {
        return new CommonPresenter();
    }

    @Override
    protected MineModule getModule() {
        return new MineModule();
    }


    private void fillUI(BindedCardData data) {
        bindedCardData = data;
        if ("1".equals(data.getBankStatus())) {
            //换卡
            linYesData.setVisibility(View.VISIBLE);
            linNoData.setVisibility(View.GONE);
            if (tv_bank_card_name != null) {
                tv_bank_card_name.setText(data.getBankValue());
            }
            if (tv_bank_card_num != null) {
                MaskUtils.setText(tv_bank_card_num, data.getBankCard());
            }
            if (tv_bank_card_user_name != null) {
                MaskUtils.setText(tv_bank_card_user_name, data.getRealName());
            }
            tvChange.setVisibility(View.VISIBLE);
        } else {
            if (StringUtils.isEmpty(data.getBankCard())) {
                linYesData.setVisibility(View.GONE);
                linNoData.setVisibility(View.VISIBLE);
            } else {
                linYesData.setVisibility(View.VISIBLE);
                linNoData.setVisibility(View.GONE);
                if (tv_bank_card_name != null) {
                    tv_bank_card_name.setText(data.getBankValue());
                }
                if (tv_bank_card_num != null) {
                    MaskUtils.setText(tv_bank_card_num, data.getBankCard());
                }
                if (tv_bank_card_user_name != null) {
                    MaskUtils.setText(tv_bank_card_user_name, data.getRealName());
                }
                tvChange.setVisibility(View.GONE);
            }
        }
    }


    @Override
    public void showResponse(int apiType,String listType, Object o) {
        if(apiType== HttpConstant.BINDED_CARD_DATA){ //获取银行卡信息
            BaseResponse<BindedCardData> baseResponse = (BaseResponse<BindedCardData>) o;
            dismissLoadingView();
                fillUI(baseResponse.getData());
        }

        else if(apiType==HttpConstant.SAVE_BANK_CARD_INFO){//添加银行卡

        }

        else if(apiType==HttpConstant.PERSON_INFO){

        }
    }

    @Override
    public void showError(Throwable e) {
        dismissLoadingView();
    }
}
